//
//  HSYShowRootViewController.m
//  高仿暴走斗图
//
//  Created by huangsongyao on 16/3/26.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "HSYShowRootViewController.h"
#import "UIScrollView+Page.h"
#import "UIApplication+Manager.h"
#import "AppDelegate.h"
#import "HSYRootViewController.h"
#import "HSYBaseCardNavigationController.h"

#define HSY_SHOW_ROOT_ANIMATION_KEY         @"ShowRootAnimationKey"
#define HSY_SHOW_ROOT_ANIMATION_DURATION    1.0f

@interface HSYShowRootImageView : UIView

@property (nonatomic, strong) UIImageView * imageView;
@property (nonatomic, assign) BOOL isBigDot;

- (instancetype)initWithFrame:(CGRect)frame image:(NSString *)image;
@end

@implementation HSYShowRootImageView

- (instancetype)initWithFrame:(CGRect)frame image:(NSString *)image {
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = CLEAR_COLOR;
        _isBigDot = NO;
        
        self.imageView = [NSObject createImgWithImage:CREATE_IMG(image)
                                               hImage:CREATE_IMG(image)
                                        clipsToBounds:YES
                                              vcModel:UIViewContentModeScaleToFill];
        
        CGFloat size = self.viewHeight/2;
        self.imageView.frame = CGRectMake(0, 0, size, size);
        [self.imageView setCenter:self.center];
        [self.imageView.layer setCornerRadius:self.imageView.viewHeight/2];
        [self addSubview:self.imageView];
    }
    return self;
}

- (void)setIsBigDot:(BOOL)isBigDot {
    
    _isBigDot = isBigDot;
    if (!self.imageView) {
        return;
    }
    
    if (isBigDot) {
        
        [self.imageView setViewSize:CGSizeMake(self.viewHeight, self.viewHeight)];
        [self.imageView setCenter:self.center];
    } else {
        
        CGFloat size = self.viewHeight/2;
        [self.imageView setViewSize:CGSizeMake(size, size)];
        [self.imageView setCenter:self.center];
    }
    
}

@end

@interface HSYShowRootDotsView : UIView

@property (nonatomic, strong) NSMutableArray *dots;

- (instancetype)initWithFrame:(CGRect)frame withDots:(NSInteger)dots;
- (void)setDotsStatusForIndex:(NSInteger)index;
@end

@implementation HSYShowRootDotsView

- (instancetype)initWithFrame:(CGRect)frame withDots:(NSInteger)dots {
    
    if (self = [super initWithFrame:frame]) {
        
        CGFloat width = self.viewWidth/dots/2;
        self.dots = [[NSMutableArray alloc] init];
        
        for (NSInteger i = 0; i < dots; i ++) {
            CGRect rect = CGRectMake(i *width, 0, width, width);
            HSYShowRootImageView *dotImage = [[HSYShowRootImageView alloc] initWithFrame:rect image:SHOWVC_DOT];
            dotImage.tag = i;
            [self addSubview:dotImage];
            if (i == 0) {
                dotImage.isBigDot = YES;
            }
            [self.dots addObject:dotImage];
        }
    }
    
    return self;
}

- (void)setDotsStatusForIndex:(NSInteger)index {

    if (!self.dots) {
        return;
    }
    
    for (NSInteger i = 0; i < self.dots.count; i ++) {
        HSYShowRootImageView *dotImage = [self.dots objectAtIndex:i];
        if (i == index) {
            dotImage.isBigDot = YES;
        } else {
            dotImage.isBigDot = NO;
        }
    }
}

@end

@interface HSYShowRootViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) HSYShowRootDotsView *dotsView;
@property (nonatomic, assign) NSInteger pages;
@property (nonatomic, strong) UIWindow *window;

- (instancetype)initWithPages:(NSInteger)pages;

@end

@implementation HSYShowRootViewController

+ (void)addWindowByPages:(NSInteger)pages {
    
    HSYShowRootViewController *vc = [[HSYShowRootViewController alloc] initWithPages:pages];
    UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    window.windowLevel = kWindowLevelTypeShowImage;
    window.rootViewController = vc;
    [window makeKeyAndVisible];
    vc.window = window;
}

- (instancetype)initWithPages:(NSInteger)pages {
    
    if (self = [super init]) {
        _pages = pages;
    }
    return self;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.view.backgroundColor =[UIColor whiteColor];
    [[UserManager shareInstance] saveUserStatusType:kUserStatusTypeNotLogin];
    
    self.scrollView = [NSObject createScrollViewWithContentOffset:CGPointMake(0, 0)
                                                  withContentSize:CGSizeMake(IPHONE_WIDTH, IPHONE_HEIGHT)
                                                 canPagingEnabled:YES
                                                 canScrollEnabled:YES
                                                       canBounces:NO
                                                         delegate:self];
    self.scrollView.frame = self.view.bounds;
    [self.view addSubview:self.scrollView];
    [self addImageForPages:self.pages];
    
    self.dotsView = [[HSYShowRootDotsView alloc] initWithFrame:CGRectMake(0, IPHONE_HEIGHT - 100, 150, 40) withDots:self.pages];
    [self.dotsView setCenter:CGPointMake(self.view.viewCenterX, self.dotsView.viewCenterY)];
    [self.view addSubview:self.dotsView];
}


- (void)addImageForPages:(NSInteger)pages {
    
    if (!self.scrollView) {
        return;
    }
    for (NSInteger i = 0; i < pages; i ++) {
        UIImageView *imageView = [NSObject createImgWithImage:CREATE_IMG(SHOWIMAGE((long)i))
                                                       hImage:CREATE_IMG(SHOWIMAGE((long)i))
                                                clipsToBounds:YES
                                                      vcModel:UIViewContentModeScaleToFill];
        imageView.frame = CGRectMake(self.scrollView.viewWidth *i, 0, self.scrollView.viewWidth, self.scrollView.viewHeight);
        [self.scrollView addSubview:imageView];
                                  
    }
    [self.scrollView setContentSize:CGSizeMake(pages *IPHONE_WIDTH, 0)];
}


#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    NSInteger currentPage = [scrollView currentPage];
    if (currentPage == self.pages - 1) {
        
        scrollView.bounces = YES;
        self.dotsView.hidden = YES;
    } else {
        
        self.dotsView.hidden = NO;
        scrollView.bounces = NO;
        [self.dotsView setDotsStatusForIndex:currentPage];
    }
    
    CGFloat lastPageLeft = (self.pages - 1) *IPHONE_WIDTH;
    if (scrollView.contentOffset.x > lastPageLeft + 50 && currentPage == (self.pages - 1)) {
        [self transitionToRootViewController];
    }
}


- (void)transitionToRootViewController {
    
    [self dismissViewControllerAnimated:YES completion:nil];
    if (self.window) {
        //渐变淡出动画
        CGFloat duration = HSY_SHOW_ROOT_ANIMATION_DURATION;
        @weakify(self);
        [UIView animateWithDuration:duration animations:^{
         
            @strongify(self);
            self.window.alpha = 0.0;
            self.window.transform = CGAffineTransformMakeScale(1.5, 1.5);
        } completion:^(BOOL finished) {
        }];
        [NSObjcGCD gcd_DelayExecutionForDelayInSeconds:(int64_t)(duration/2 * NSEC_PER_SEC) dispatchCompletion:^{
            @strongify(self);
            self.window.rootViewController = nil;
            [self.window removeFromSuperview];
        }];
    }
}

@end
